package org.niux.AutoLoginQQ.ui;

import java.io.InputStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.niux.AutoLoginQQ.common.Constant;

public class AddQQ extends Dialog {

	protected Object result;
	protected Shell shlqq;
	private Main gui;
	private Text text;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public AddQQ(Shell parent, int style ,Main gui) {
		super(parent, style);
		setText("添加QQ号码");
		this.gui = gui;
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();

		shlqq.open();
		shlqq.layout();
		Display display = getParent().getDisplay();
		while (!shlqq.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shlqq = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.SYSTEM_MODAL);
		shlqq.setBackground(SWTResourceManager.getColor(173, 216, 230));
		shlqq.setSize(334, 285);
		shlqq.setText("添加QQ号码");

		// 窗口居中
		int width = shlqq.getMonitor().getClientArea().width;
		int height = shlqq.getMonitor().getClientArea().height;

		int x = shlqq.getSize().x;
		int y = shlqq.getSize().y;

		if (x > width) {
			shlqq.getSize().x = width;
		}

		if (y > height) {
			shlqq.getSize().y = height;
		}

		shlqq.setLocation((width - x) / 2, (height - y) / 2);

		shlqq.open();
		shlqq.layout();

		InputStream is=this.getClass().getResourceAsStream("icon/ico.png");
		Image image = new Image(Display.getDefault(),is);
		shlqq.setImage(image);

		Button okBtn = new Button(shlqq, SWT.NONE);
		shlqq.setDefaultButton(okBtn);
		
		okBtn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String[] numTextArray = text.getText().split("\r\n");
				
				for(String s : numTextArray){
					if(!s.contains(Constant.SPLIT_STR)){
						alert("格式错误，请输入如“QQ号码"+Constant.SPLIT_STR+"QQ密码”格式的内容");
						return;
					} else {
						String num = s.split(Constant.SPLIT_STR)[0].replace(" ", "");
						String pwd = s.split(Constant.SPLIT_STR)[1].replace(" ", "");
						addQQToTable(num,pwd);
					}
				}
				
				text.setText("");
			}
		});
		okBtn.setBounds(41, 220, 80, 27);
		okBtn.setText("确定");

		Button cancleBtn = new Button(shlqq, SWT.NONE);
		cancleBtn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shlqq.close();
			}
		});
		cancleBtn.setBounds(204, 220, 80, 27);
		cancleBtn.setText("取消");
		
		text = new Text(shlqq, SWT.BORDER | SWT.V_SCROLL | SWT.MULTI);
		text.setBounds(10, 10, 308, 195);
	}

	/**
	 * 
	 * 方法概述：把新增的QQ号码添加到列表中
	 * 描述：把新增的QQ号码添加到列表中
	 * @param qqNum
	 * @param password
	 * 返回类型：void
	 * 修改人：梁铭显
	 * 修改时间：2012-9-14
	 */
	public void addQQToTable(final String qqNum,final String password){
		if(!Constant.QQ_LIST.containsKey(qqNum)){
			Display.getDefault().asyncExec(new Runnable() {
				public void run() {
					gui.addQQToTable(qqNum, password);
				}
			});
		}
	}
	
	/**
	 * 
	 * 方法概述：提示信息对话框
	 * 描述：提示信息对话框
	 * @param info
	 * 返回类型：void
	 * 修改人：梁铭显
	 * 修改时间：2012-9-12
	 */
	public void alert(final String info){
		Display.getDefault().asyncExec(new Runnable() {
			public void run() {
				gui.alert(info);
			}
		});
	}
}
